<template>
    <div class="user_conter">
        <div class="user_img">
            <img src="https://yanxuan.nosdn.127.net/static-union/164793255107785e.png" alt="">
        </div>
        <div class="userinfo">
            <van-form @submit="onSubmit">
                <van-cell-group inset>
                    <van-field v-model="phone" name="用户名" label="用户名" placeholder="用户名"
                        :rules="[{ required: true, message: '请填写用户名' }]" />
                    <van-field v-model="code" name="验证码" label="验证码" placeholder="验证码"
                        :rules="[{ required: true, message: '请填写验证码' }]" />
                    <van-field v-model="password" type="password" name="密码" label="密码" placeholder="密码"
                        :rules="[{ required: true, message: '请填写密码' }]" />
                    <van-field v-model="repassword" type="password" name="确认密码" label="确认密码" placeholder="确认密码"
                        :rules="[{ required: true, message: '请填确认密码' }]" />
                </van-cell-group>
                <van-checkbox v-model="isArr" icon-size="12px"
                    style="font-size: 12px; margin-left: 30px; margin-top: 10px; ">同意用户协议</van-checkbox>
                <van-button class="btn" round type="success" size="mini" @click="VerificationCode">获取验证码</van-button>

                <div style="margin: 16px;">
                    <van-button round block type="primary" native-type="submit">
                        注册账号
                    </van-button>
                </div>
            </van-form>
        </div>

    </div>
</template>

<script  setup lang='ts'>
import { ref } from 'vue'
import userApi from '../../../api/userinfo'
import { ElMessage } from 'element-plus';

import { useRouter } from 'vue-router'
const phone = ref('');
const password = ref('');
const code = ref<any>('')
const repassword = ref('')
const isArr = ref(false)
const router = useRouter()
const onSubmit = async () => {
    if (isArr.value) {
        let formData = {
            phone: phone.value,
            password: password.value,
            code: code.value,
        }
        try {
            let res = await userApi.PostuserKode(formData)
            ElMessage.success('注册成功')
            router.push('User')
        } catch (error) {
            console.log(error
            );

        }
    } else {
        ElMessage.error('请勾选用户协议')

    }

}

const VerificationCode = async () => {

    if (!phone.value) {
        ElMessage.error('请输入手机号')
        return
    }
    try {
        let res = await userApi.GetVerificationCode(phone.value)
        code.value = res.data
    } catch (error) {
        console.log(error);

    }
}
</script>

<style scoped lang="less">
.user_conter {
    width: 100%;
    min-height: 100%;
    padding: 0 10px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    align-items: center;

    .user_img {
        width: 134px;
        height: 45px;
        text-align: center;
        margin-top: 50px;

        img {
            width: 100%;
            height: 100%;
        }
    }

    .userinfo {
        margin-top: 80px;
        position: relative;

        .btn {
            position: absolute;
            right: 30px;
            bottom: 76px;
        }
    }

    .gologin {
        width: 100%;
        height: 20px;
        margin-top: 50px;
        text-align: center;
        font-size: 12px;
    }
}
</style>